<?php

namespace Beerus\Modules\Designer\Actions;

use Doraemon\model\Designer\RenewIntentionLog as mDRILog;
use Doraemon\model\Designer\RenewIntention;
use Illuminate\Http\Request;
use Encore\Admin\Actions\RowAction;
use Encore\Admin\Facades\Admin;

class AddRenewIntention extends RowAction
{
    public $name = '录入续费意愿';

    public function handle($iId, Request $request)
    {
        $iIntentionLevel = $request->post('form_intention_level');
        $sIntentionReason = $request->post('form_intention_reason');
        $iIntentionUid = $request->post('form_intention_uid');
        if (empty($iIntentionLevel)) {
            return $this->response()->error('请选择续费意愿～');
        }
        if (mb_strlen($sIntentionReason) > 200) {
            return $this->response()->error('意愿理由超过200字～');
        }
        $aData = [
            'uid' => $iIntentionUid,
            'intention_level' => $iIntentionLevel,
            'intention_reason' => $sIntentionReason,
            'admin_id' => Admin::user()->id,
            'admin_name' => Admin::user()->username
        ];
        if (!mDRILog::addInfo($aData)) {
            return $this->response()->error('续费意愿更新失败～');
        }
        $aIntentionInfo = RenewIntention::getItemByUid($iIntentionUid);
        if (!empty($aIntentionInfo)) {
            $aUpdateData = [];
            if ($aIntentionInfo['intention_level'] != $iIntentionLevel) {
                $aUpdateData['intention_level'] = $iIntentionLevel;
            }
            if ($aIntentionInfo['intention_reason'] != $sIntentionReason) {
                $aUpdateData['intention_reason'] = $sIntentionReason;
            }
            if (!empty($aUpdateData) && !RenewIntention::updateInfo($aUpdateData, ['uid'=>$iIntentionUid])) {
                return $this->response()->error('续费意愿更新失败～');
            }
        } else {
            $aAddData = [
                'uid' => $iIntentionUid,
                'intention_level' => $iIntentionLevel,
                'intention_reason' => $sIntentionReason,
            ];
            if (!RenewIntention::addInfo($aAddData)) {
                return $this->response()->error('续费意愿添加失败～');
            }
        }
        return $this->response()->success('续费意愿已更新')->refresh();
    }

    public function retrieveModel(Request $request)
    {
        if (!$key = $request->get('_key')) {
            return false;
        }

        return $key;
    }

    public function form()
    {
        $iIntentionUid = $this->row->uid;
        $sNick = $this->row->nick;
        $iDefaultIntentionLevel = 0;
        $sDefaultIntentionReason = '';
        $aIntentionInfo = RenewIntention::getItemByUid($iIntentionUid);
        if (!empty($aIntentionInfo)) {
            $iDefaultIntentionLevel = $aIntentionInfo['intention_level'];
            $sDefaultIntentionReason = $aIntentionInfo['intention_reason'];
        }
        $sNick = !empty($sNick) ? $sNick : '设计师UID:'.$iIntentionUid;
        $this->hidden('form_intention_uid', 'uid')->value($iIntentionUid);
        $sLabel = '请录入设计师「'.$sNick.'」续费意愿';
        $this->radio('form_intention_level', $sLabel)
            ->options(mDRILog::INTENTION_LEVEL_LIST)
            ->default($iDefaultIntentionLevel);
        $this->textarea('form_intention_reason', '理由')
            ->default($sDefaultIntentionReason);
    }
}
